program Polinom;

{$APPTYPE CONSOLE}
type
pelement = ^element;
element = record
  koef : integer;
  degr : integer;
  next : pelement;
end;
var
head1 : pelement;
head2 : pelement;
head3 : pelement;
i, j : integer;
current1 : pelement;
current2 : pelement;
n1, n2 : integer;
k, d : integer;
procedure AddToBegin(var head : pelement; k : integer; d : integer);
var
  tmp : pelement;
begin
  tmp := head;
  new(head);
  head^.koef := k;
  head^.degr := d;
  head^.next := tmp;
end;

procedure AddToEnd(var head : pelement; k : integer; d : integer);
var
  current : pelement;
begin
  if(head<>nil) then
  begin
    current := head;
    while (current^.next <> nil) do
      current := current^.next;
    new(current^.next);
    current^.next^.koef := k;
    current^.next^.degr := d;
    current^.next^.next := nil;
  end
  else
  begin
    AddToBegin(head, k, d);
  end;
end;

procedure MultPol(head : pelement; k : integer; d : integer);
var
  current : pelement;
begin
  current := head;
  while (current^.next <> nil) do
  begin
    current := current^.next;
    current^.degr := current^.degr+d;
    current^.koef := current^.koef+k;
  end;
end;

begin
  k := 0;
  d := 0;
  new(head1);
  head1 := nil;
  new(head2);
  head2 := nil;
  new(head3);
  head3 := nil;

  readln(n1);
  for i:=1 to n1 do
  begin
    readln(k, d);
    AddToEnd(head1, k, d);
  end;
  readln(n2);
  for i:=1 to n2 do
  begin
    readln(k, d);
    AddToEnd(head2, k, d);
  end;
  current1 := head1;
  current2 := head2;
  for i:= 1 to n1 do
  begin
    if (current1 <> head1) then
      current1 := current1^.next;
    for j:=1 to n2 do
    begin
      if (current2 <> head2) then
        current2 := current2^.next;
      if (current2^.degr = current1^.degr) then
      begin
        d := current1^.degr;
        k := k + current2^.koef+current1^.koef;
      end;
    end;
    AddToBegin(head3, k, d);
  end;



end.
 